<?xml version="1.0" encoding="UTF-8" ?>

<!-- New document created with EditiX at Wed Dec 21 01:09:35 WET 2011 -->

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

	<xsl:output method="xml" indent="yes"/>
	
	<xsl:template match="/">
		<games>
			<xsl:apply-templates select="/games/game"/>
		</games>
	</xsl:template>


	<xsl:template match="game">
		<game>
			<xsl:attribute name="id">
				<xsl:value-of select="@id" />
			</xsl:attribute>
			<date><xsl:value-of select="td//div[@class = 'fj_header_datatv']/text()" /></date>
			<stadium><xsl:value-of select="td//div[@class = 'fj_header_estadioarbitro']/a[1]/text()" /></stadium>
			<referee><xsl:value-of select="td//div[@class = 'fj_header_estadioarbitro']/a[2]/text()" /></referee>
			<score>
				<xsl:value-of select="td//td[@class = 'fj_marcador_txt']/text()" />
			</score>
			<home_team>
				<team_name>
					<xsl:value-of select="td//div[@class = 'fj_equipa_casa']/a/text()" />
				</team_name>
				<lineup>
					<xsl:apply-templates select="home_team/td[@class = 'fj_ficha_equipa_casa']/table/tbody/tr"/>
				</lineup>
				<substitutes>
					<xsl:apply-templates select="home_team/td[@class = 'fj_ficha_equipa_casa_suplente']/table/tbody/tr"/>
				</substitutes>
			</home_team>
			<away_team>
				<team_name>
					<xsl:value-of select="td//div[@class = 'fj_equipa_fora']/a/text()" />
				</team_name>
				<lineup>
					<xsl:apply-templates select="away_team/td[@class = 'fj_ficha_equipa_fora']/table/tbody/tr"/>
				</lineup>
				<substitutes>
					<xsl:apply-templates select="away_team/td[@class = 'fj_ficha_equipa_fora_suplente']/table/tbody/tr"/>
				</substitutes>
			</away_team>
		</game>
	</xsl:template>

	<!-- PLAYER TEMPLATE -->
	<xsl:template  match="tr">
		<player>
			<name>
				<xsl:value-of select="td[@class='fj_ficha_nome']/a[2]/text()" />
			</name>
			<number>
				<xsl:value-of select="td[@class='fj_ficha_numero']/text()" />
			</number>
			<xsl:if test="td[@class='fj_ficha_subs']/div">
				<substitution>
					<type>
						<xsl:value-of select="substring-after(td[@class='fj_ficha_subs']/div/@class, 'fj_ficha_')" />
					</type>
					<minute>
						<xsl:value-of select="td[@class='fj_ficha_subs']/div/text()" />
					</minute>
				</substitution>
			</xsl:if>
			<xsl:if test=".//td[@class='fj_ficha_golo']">
				<goals>
					<minute>
						<xsl:value-of select=".//td[@class='fj_ficha_golo']/text()" />
					</minute>
				</goals>
			</xsl:if>
			<xsl:apply-templates select=".//td[@class='fj_ficha_amarelo' or @class='fj_ficha_vermelho']" />

		</player>
	</xsl:template>

	<xsl:template match="td">
		<discipline>
			<type>
				<xsl:value-of select="substring-after(./@class, 'fj_ficha_')" />
			</type>
			<minute>
				<xsl:value-of select="./text()" />
			</minute>
		</discipline>
	</xsl:template>

	
</xsl:stylesheet>
